package com.yanqu.road.server.cmd.activity.chuhan;

import com.yanqu.road.logic.activity.chuhan.ChuHanConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChuHanProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ForbiddenWordMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.tongbing.ChuHanMgr;
import com.yanqu.road.server.protocol.*;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_CHUHAN_COMMANDER_SET, desc = "指挥官设置")
public class ChuHanCommanderSetCmd extends AbstractCommand {

    private ChuHanProto.ChuHanCommanderSetReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = ChuHanProto.ChuHanCommanderSetReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ChuHanConfig config = ChuHanMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        if (reqMsg.hasNotice() && ForbiddenWordMgr.isForbidden(reqMsg.getNotice())) {
            ret = GameErrorCode.E_FORBIDDEN_WORD;
            return;
        }

        ChuHanProto.CrossChuHanCommanderSetReqMsg.Builder builder = ChuHanProto.CrossChuHanCommanderSetReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setCommanderSetReqMsg(reqMsg.toBuilder());
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_CHUHAN_COMMANDER_SET, builder, config.getActivityInfo().getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            ChuHanProto.ChuHanCommanderSetRespMsg.Builder respMsg = ChuHanProto.ChuHanCommanderSetRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.S_CHUHAN_COMMANDER_SET%20000, respMsg);
        }
    }
}
